package com.lczyfz.istep.modules.openstack.api.neutron.impl.firewall;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.firewall.FireWalls;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWall;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class FireWallImpl implements FireWalls {
	
	public RequestFactory factory = new RequestFactory("neutron");
	
	public static void main(String[] args){
		
		FireWallImpl impl = new FireWallImpl();
		

//		List<String> routerIds = new ArrayList<String>();
//		impl.getFireWalls();
//		FireWall firewall = new FireWall();
//		firewall.setName("fire1");
//		firewall.setAdminStateUp("true");
//		firewall.setDescription("hehe");
//		firewall.setFirewallPolicyId("6a5f537e-7516-4bde-bce6-7c55ea84a9e7");
//		routerIds.add("da13ba91-2fe6-4ad3-8649-751a01269ece");
//		firewall.setRouterIds(routerIds);
//		impl.createFireWall(firewall);

		
		/*�鿴����ǽ
		FireWall firewall = impl.getFireWallDetail("31ac894a-c8c2-4cf2-825e-950a85d1c332");
		System.out.println(firewall);
		*/
		
		//ɾ������ǽ 
//		impl.deleteFireWall("834b4a8d-759b-4ce9-b389-2b0a5e553b2b");
		
		
		//�оٳ����еķ���ǽ
		List<FireWall> firewalls = impl.getFireWalls();
		for(FireWall firewall1 : firewalls){
			System.out.println(firewall1.getFirewallId()+" "+firewall1.getName());
		}
		
		
		/* ���·���ǽ 
		FireWall firewall = new FireWall();
		firewall.setName("lgh-modify");
		firewall.setAdminStateUp("false");
		firewall.setDescription("hehe-modify");
		firewall.setFirewallId("62a3c488-dab3-45a7-9f6c-b5febd6b96bf");
		
		impl.updateFireWall("62a3c488-dab3-45a7-9f6c-b5febd6b96bf", firewall);
		*/
	}
	
	@Override
	public List<FireWall> getFireWalls() {
		
		List<FireWall> firewalls = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewalls");
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);
		
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		JSONObject body = results.getResponseBody();
		firewalls = NeutronParse.resolveFireWalls(body);
		return firewalls;
		
	}

	@Override
	public FireWall getFireWallDetail(String fireWallId) {
		
		FireWall firewall = new FireWall();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewalls/" + fireWallId);
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);

		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		JSONObject body = results.getResponseBody();
		firewall = NeutronParse.resolveFireWallDetail(body);
		return firewall;
		
	}

	@Override
	public FireWall createFireWall(FireWall fireWall) {

		JSONObject params = NeutronParse.packageFireWall(fireWall);
		System.out.println(params.toString());
		
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewalls");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);

		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		
		JSONObject res = results.getResponseBody();
		FireWall createdfirewall = new FireWall();
		createdfirewall = NeutronParse.resolveFireWallDetail(res);
		return createdfirewall;
		
	}

	@Override
	public void deleteFireWall(String fireWallId) {
		
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewalls/" + fireWallId);
		request.setRequestType(RequestType.DELETE);
		Response results = HTTP.getConnectionResult(request);
		
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() ){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
		
	}

	@Override
	public FireWall updateFireWall(String fireWallId, FireWall fireWall) {
		
		
		JSONObject params = NeutronParse.packageFireWall(fireWall);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewalls/" + fireWallId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		
		JSONObject res = results.getResponseBody();
		FireWall updatedfirewall = new FireWall();
		updatedfirewall = NeutronParse.resolveFireWallDetail(res);
		return updatedfirewall;
		
	}

}
